package com.nce.web;

import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.io.OutputStream;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Collections;
import java.util.Date;
import java.util.List;

import org.apache.commons.lang.StringUtils;
import org.apache.struts2.convention.annotation.Namespace;
import org.apache.struts2.convention.annotation.Result;
import org.apache.struts2.convention.annotation.Results;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springside.modules.orm.Page;
import org.springside.modules.orm.PropertyFilter;
import org.springside.modules.utils.web.struts2.Struts2Utils;

import com.nce.entity.BaseNews;
import com.nce.entity.BbsBlog;
import com.nce.entity.Mblog;
import com.nce.entity.News;
import com.nce.entity.Report;
import com.nce.service.SearchService;
import com.nce.util.LoginUserUtil;
import com.opensymphony.xwork2.ActionSupport;

@Namespace("/s")
@Results({ @Result(name = CrudActionSupport.RELOAD, location = "search.action", type = "redirect") })
public class SearchAction extends ActionSupport {

	private static final long serialVersionUID = -6999904901932084228L;
	private static Logger logger = LoggerFactory.getLogger(SearchAction.class);
	@Autowired
	private SearchService searchService;

	private Page<BaseNews> baseNewsPage = new Page<BaseNews>(20);
	private Page<News> newsPage = new Page<News>(20);
	private Page<Mblog> mblogPage = new Page<Mblog>(20);
	private Page<BbsBlog> bbsBlogPage = new Page<BbsBlog>(20);
	private String words;
	private boolean isTitle = false;
	
	private Page<Report> reportPage = new Page<Report>(10000);
	
	


	public Page<Report> getReportPage() {
		return reportPage;
	}

	public void setReportPage(Page<Report> reportPage) {
		this.reportPage = reportPage;
	}

	public String getWords() {
		return words;
	}

	public void setWords(String words) {
		this.words = words;
	}

	public boolean getIsTitle() {
		return isTitle;
	}

	public void setIsTitle(boolean isTitle) {
		this.isTitle = isTitle;
	}

	public Page<News> getNewsPage() {
		return newsPage;
	}

	public void setNewsPage(Page<News> newsPage) {
		this.newsPage = newsPage;
	}

	public Page<Mblog> getMblogPage() {
		return mblogPage;
	}

	public void setMblogPage(Page<Mblog> mblogPage) {
		this.mblogPage = mblogPage;
	}

	public Page<BbsBlog> getBbsBlogPage() {
		return bbsBlogPage;
	}

	public void setBbsBlogPage(Page<BbsBlog> bbsBlogPage) {
		this.bbsBlogPage = bbsBlogPage;
	}

	public Page<BaseNews> getBaseNewsPage() {
		return baseNewsPage;
	}

	public void setBaseNewsPage(Page<BaseNews> baseNewsPage) {
		this.baseNewsPage = baseNewsPage;
	}

	private List<PropertyFilter> initFilters() {
		List<PropertyFilter> filters = PropertyFilter.buildFromHttpRequest(Struts2Utils.getRequest());
		PropertyFilter pf = new PropertyFilter("EQL_user.id", LoginUserUtil.getUser().getId().toString());
		filters.add(pf);
		
//		if(StringUtils.isEmpty(words)){
//			words = Struts2Utils.getParameter("words");
//		}
		if (StringUtils.isNotEmpty(words)) {
			// 标题搜索或者内容搜索
			String type = Struts2Utils.getParameter("type");
			if (StringUtils.isNotEmpty(type) && type.equals("title")) {
				pf = new PropertyFilter("LIKES_title", words);
				filters.add(pf);
				isTitle = true;
			} else {
				pf = new PropertyFilter("LIKES_content_OR_title", words);
				filters.add(pf);				
			}
		}

		return filters;
	}

	public String getNews() {
		List<PropertyFilter> filters = initFilters();
		// 设置默认排序方式
		if (!newsPage.isOrderBySetted()) {
			newsPage.setOrderBy("tmSpider");
			newsPage.setOrder(Page.DESC);
		}
		newsPage = searchService.searchNews(newsPage, filters);	
		
		processNewsResult();
		
		return "news";
	}
	
	
	private void processBaseNewsResult() {

		for (BaseNews news : baseNewsPage.getResult()) {
			String newContent = news.getContent();
			String title = news.getTitle();
			if (newContent !=null && newContent.length() > 200) {
				newContent = (newContent.substring(0, 200) + "......");
			}
			if(title!=null && title.length() > 24){
				title = title.substring(0,24)+"......";
			}
			if (StringUtils.isNotEmpty(words)) {
				// 只标红内容搜索
				if (isTitle) {
					title = news.getTitle().replace(words, "<span style='color:#CD4F39;'>" + words + "</span>");
				} else {
					if(StringUtils.isNotEmpty(news.getTitle())){
						title = news.getTitle().replace(words, "<span style='color:#CD4F39;'>" + words + "</span>");
					}
					if(StringUtils.isNotEmpty(newContent)){
						newContent = newContent.replace(words, "<span style='color:#CD4F39;'>" + words + "</span>");
					}
				}
			}
			news.setViewContent(newContent);
			news.setViewTitle(title);
			
			if (news.getTmSpider().before(LoginUserUtil.getUser().getLoginTime())) {
				news.setIsNew("false");
			}
		}
	}

	/**
	 * 搜索词标红
	 */
	private void processNewsResult() {

		for (News news : newsPage.getResult()) {
			String newContent = news.getContent();
			String title = news.getTitle();
			if (newContent !=null && newContent.length() > 200) {
				newContent = (newContent.substring(0, 200) + "......");
			}
			if(title!=null && title.length() > 24){
				title = title.substring(0,24)+"......";
			}
			if (StringUtils.isNotEmpty(words)) {
				// 只标红内容搜索
				if (isTitle) {
					title = news.getTitle().replace(words, "<span style='color:#CD4F39;'>" + words + "</span>");
				} else {
					if(StringUtils.isNotEmpty(news.getTitle())){
						title = news.getTitle().replace(words, "<span style='color:#CD4F39;'>" + words + "</span>");
					}
					if(StringUtils.isNotEmpty(newContent)){
						newContent = newContent.replace(words, "<span style='color:#CD4F39;'>" + words + "</span>");
					}
				}
			}
			news.setViewContent(newContent);
			news.setViewTitle(title);
			
			if (news.getTmSpider().before(LoginUserUtil.getUser().getLoginTime())) {
				news.setIsNew("false");
			}
		}
	}

	/**
	 * 搜索词标红
	 */
	private void processBbsBlogResult() {

		for (BbsBlog news : bbsBlogPage.getResult()) {
			String newContent = news.getContent();
			String title = news.getTitle();
			if (newContent !=null && newContent.length() > 200) {
				newContent = (newContent.substring(0, 200) + "......");
			}
			if(title!=null && title.length() > 24){
				title = title.substring(0,24)+"......";
			}
			if (StringUtils.isNotEmpty(words)) {
				// 只标红内容搜索
				if (isTitle) {
					title = news.getTitle().replace(words, "<span style='color:#CD4F39;'>" + words + "</span>");
				} else {
					if(StringUtils.isNotEmpty(news.getTitle())){
						title = news.getTitle().replace(words, "<span style='color:#CD4F39;'>" + words + "</span>");
					}
					if(StringUtils.isNotEmpty(newContent)){
						newContent = newContent.replace(words, "<span style='color:#CD4F39;'>" + words + "</span>");
					}
				}
			}
			news.setViewContent(newContent);
			news.setViewTitle(title);
			if (news.getTmSpider().before(LoginUserUtil.getUser().getLoginTime())) {
				news.setIsNew("false");
			}
		}
	}

	/**
	 * 搜索词标红
	 */
	private void processMBlogResult() {

		for (Mblog news : mblogPage.getResult()) {
			String newContent = news.getContent();
			String title = news.getTitle();
			if (newContent !=null && newContent.length() > 200) {
				newContent = (newContent.substring(0, 200) + "......");
			}
			if(title!=null && title.length() > 24){
				title = title.substring(0,24)+"......";
			}
			if (StringUtils.isNotEmpty(words)) {
				// 只标红内容搜索
				if (isTitle) {
					title = news.getTitle().replace(words, "<span style='color:#CD4F39;'>" + words + "</span>");
				} else {
					if(StringUtils.isNotEmpty(news.getTitle())){
						title = news.getTitle().replace(words, "<span style='color:#CD4F39;'>" + words + "</span>");
					}
					if(StringUtils.isNotEmpty(newContent)){
						newContent = newContent.replace(words, "<span style='color:#CD4F39;'>" + words + "</span>");
					}
				}
			}
			news.setViewContent(newContent);
			news.setViewTitle(title);
			if (news.getTmSpider().before(LoginUserUtil.getUser().getLoginTime())) {
				news.setIsNew("false");
			}
		}
	}

	public String getBbsblog() {
		List<PropertyFilter> filters = initFilters();
		// 设置默认排序方式
		if (!bbsBlogPage.isOrderBySetted()) {
			bbsBlogPage.setOrderBy("tmSpider");
			bbsBlogPage.setOrder(Page.DESC);
		}
		bbsBlogPage = searchService.searchBbsBlog(bbsBlogPage, filters);
		processBbsBlogResult();
		
		return "bbsblog";
	}

	public String getMblog() {

		List<PropertyFilter> filters = initFilters();
		// 设置默认排序方式
		if (!mblogPage.isOrderBySetted()) {
			mblogPage.setOrderBy("tmSpider");
			mblogPage.setOrder(Page.DESC);
		}
		mblogPage = searchService.searchMblog(mblogPage, filters);
		processMBlogResult();
		
		return "mblog";
	}

	@SuppressWarnings("unchecked")
	public String getBaseNews() {

		List<PropertyFilter> filters = initFilters();
		Date beforWeek = new Date(LoginUserUtil.getUser().getLoginTime().getTime()-1000*3600*24*7);
		String timeStr = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(beforWeek);
		PropertyFilter pf = new PropertyFilter("GTD_tmPost",timeStr);
		filters.add(pf);
		List<Mblog> mblogList = searchService.searchMblog(filters);
		List<News> newsList = searchService.searchNews(filters);
		List<BbsBlog> bbsList = searchService.searchBbsblog(filters);

		List<BaseNews> basenews = new ArrayList<BaseNews>();
		basenews.addAll(mblogList);
		basenews.addAll(newsList);
		basenews.addAll(bbsList);		
		Collections.sort(basenews);
		baseNewsPage.setTotalCount(basenews.size());
		int pageno = baseNewsPage.getPageNo();
		int pagesize = baseNewsPage.getPageSize();
		int fromidx = 0;

		if (pageno > 1) {
			fromidx = pagesize * (pageno - 1) - 1;
		}
		int toidx = fromidx + pagesize;
		if (toidx > baseNewsPage.getTotalCount() - 1 ) {
			toidx = (int) baseNewsPage.getTotalCount();
		}
		if(toidx < 0){
			toidx = 0;
		}
		List<BaseNews> result = basenews.subList(fromidx, toidx);
		baseNewsPage.setResult(result);
		processBaseNewsResult();
		
		return "basenews";
	}

	public String exportNews() {

		List<PropertyFilter> filters = initFilters();

		Struts2Utils.getResponse().setContentType("application/download;charset=UTF-8");
		Struts2Utils.getResponse().setHeader("Cache-Control", "no-stroe,no-cache");
		Struts2Utils.getResponse().setHeader("Content-disposition", "attachment;filename=\"news_"  + ".csv\"");
		OutputStream o = null;
		try {

			o = Struts2Utils.getResponse().getOutputStream();
			newsPage = new Page<News>(150);
			// 设置默认排序方式
			if (!newsPage.isOrderBySetted()) {
				newsPage.setOrderBy("tmSpider");
				newsPage.setOrder(Page.DESC);
			}
			newsPage = searchService.searchNews(newsPage, filters);	
			//List<News> newsList = searchService.searchNews(filters);
			String result = "标题,发文媒体,发文时间,新闻内容摘要,\r\n";
			for (News news : newsPage.getResult()) {
				result += news.formatForExport();
			}

			byte b[] = result.getBytes();
			o.write(b);

		} catch (IOException e) {
			logger.info("导出所有资讯出错" + e.getMessage(), e);
		} finally {
			try {
				o.close();
			} catch (IOException e) {
				logger.info("导出所有资讯出错" + e.getMessage(), e);
			}
		}
	
		return null;
	}

	public String exportMblog() {

		List<PropertyFilter> filters = initFilters();
	

		Struts2Utils.getResponse().setContentType("application/download;charset=UTF-8");
		Struts2Utils.getResponse().setHeader("Cache-Control", "no-stroe,no-cache");
		Struts2Utils.getResponse()
				.setHeader("Content-disposition", "attachment;filename=\"weibo_" +".csv\"");
		OutputStream o = null;
		try {

			o = Struts2Utils.getResponse().getOutputStream();
			mblogPage = new Page<Mblog>(150);
			// 设置默认排序方式
			if (!mblogPage.isOrderBySetted()) {
				mblogPage.setOrderBy("tmSpider");
				mblogPage.setOrder(Page.DESC);
			}
			mblogPage = searchService.searchMblog(mblogPage,filters);
			//List<Mblog> newsList = searchService.searchMblog(filters);
			String result = "发文人,微博内容,发文时间,来源,转发数,评论数,收藏数,\r\n";
			for (Mblog news : mblogPage.getResult()) {
				result += news.formatForExport();
			}

			byte b[] = result.getBytes();
			o.write(b);

		} catch (IOException e) {
			logger.info("导出微博出错" + e.getMessage(), e);
		} finally {
			try {
				o.close();
			} catch (IOException e) {
				logger.info("导出微博出错" + e.getMessage(), e);
			}
		}
	
		return null;
	}

	public String exportBbsblog() {

		List<PropertyFilter> filters = initFilters();
		
		
		Struts2Utils.getResponse().setContentType("application/download;charset=UTF-8");
		Struts2Utils.getResponse().setHeader("Cache-Control", "no-stroe,no-cache");
		Struts2Utils.getResponse()
				.setHeader("Content-disposition", "attachment;filename=\"bbs_blog_"  + ".csv\"");
		OutputStream o = null;
		try {

			o = Struts2Utils.getResponse().getOutputStream();
			bbsBlogPage = new Page<BbsBlog>(150);
			// 设置默认排序方式
			if (!bbsBlogPage.isOrderBySetted()) {
				bbsBlogPage.setOrderBy("tmSpider");
				bbsBlogPage.setOrder(Page.DESC);
			}
			bbsBlogPage = searchService.searchBbsBlog(bbsBlogPage, filters);
			//List<BbsBlog> newsList = searchService.searchBbsblog(filters);
			
			String result = "标题,发文媒体,发文时间,浏览数,回复数,作者,内容摘要\r\n";
			for (BbsBlog news : bbsBlogPage.getResult()) {
				result += news.formatForExport();

			}

			byte b[] = result.getBytes();
			o.write(b);

		} catch (IOException e) {
			logger.info("导出论坛文章出错" + e.getMessage(), e);
		} finally {
			try {
				o.close();
			} catch (IOException e) {
				logger.info("导出论坛文章出错" + e.getMessage(), e);
			}
		}
		
		return null;
	}
	
	
	public String exportBaseNews() {

		List<PropertyFilter> filters = initFilters();
		Date beforWeek = new Date(LoginUserUtil.getUser().getLoginTime().getTime()-1000*3600*24*7);
		String timeStr = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(beforWeek);
		PropertyFilter pf = new PropertyFilter("GTD_tmPost",timeStr);
		filters.add(pf);
		newsPage = new Page<News>(150);		
		newsPage = searchService.searchNews(newsPage, filters);	
		mblogPage = new Page<Mblog>(150);		
		mblogPage = searchService.searchMblog(mblogPage,filters);
		bbsBlogPage = new Page<BbsBlog>(150);		
		bbsBlogPage = searchService.searchBbsBlog(bbsBlogPage, filters);

		List<BaseNews> basenews = new ArrayList<BaseNews>();
		basenews.addAll(newsPage.getResult());
		basenews.addAll(mblogPage.getResult());
		basenews.addAll(bbsBlogPage.getResult());		
		Collections.sort(basenews);
		if(basenews.size()>150){
			basenews = basenews.subList(0,150);
		}
		
		Struts2Utils.getResponse().setContentType("application/download;charset=UTF-8");
		Struts2Utils.getResponse().setHeader("Cache-Control", "no-stroe,no-cache");
		Struts2Utils.getResponse()
				.setHeader("Content-disposition", "attachment;filename=\"all"  + ".csv\"");
		OutputStream o = null;
		try {

			o = Struts2Utils.getResponse().getOutputStream();
			bbsBlogPage = new Page<BbsBlog>(150);
			bbsBlogPage = searchService.searchBbsBlog(bbsBlogPage, filters);
			//List<BbsBlog> newsList = searchService.searchBbsblog(filters);
			
			String result = "标题,发文媒体,发文时间,内容摘要\r\n";
			for (BaseNews news : basenews) {
				result += news.formatBaseNewsForExport();
			}

			byte b[] = result.getBytes();
			o.write(b);

		} catch (IOException e) {
			logger.info("导出论坛文章出错" + e.getMessage(), e);
		} finally {
			try {
				o.close();
			} catch (IOException e) {
				logger.info("导出论坛文章出错" + e.getMessage(), e);
			}
		}
		
		return null;
	}
	
	
	public String export(){
		String content = Struts2Utils.getParameter("content");
		if(content!=null){
			if(content.equals("news")){
				return exportNews2File();
			}else if(content.equals("mblog")){
				return exportMblog2File();
			}else if(content.equals("bbsblog")){
				return exportBbsblog2File();
			}else{
				return exportBaseNews2File();
			}
		}else{
			return exportBaseNews2File();
		}
	}
	
	public String reports(){
		List<PropertyFilter> filters = PropertyFilter.buildFromHttpRequest(Struts2Utils.getRequest());
		PropertyFilter pf = new PropertyFilter("EQL_user.id", LoginUserUtil.getUser().getId().toString());
		filters.add(pf);
		
		Calendar t = Calendar.getInstance();
    	t.add(Calendar.MONTH,   -1); 		
		String timeStr = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(t.getTime());
		pf = new PropertyFilter("GTD_cdate",timeStr);
		filters.add(pf);
		
		if (!reportPage.isOrderBySetted()) {
			reportPage.setOrderBy("cdate");
			reportPage.setOrder(Page.DESC);
		}
		reportPage = searchService.findReports(filters,reportPage);
		return "reports";
	}
	
	public String exportBaseNews2File() {		

		List<PropertyFilter> filters = initFilters();
		String sdateStr = Struts2Utils.getParameter("sdate");
		String edateStr = Struts2Utils.getParameter("edate");
		PropertyFilter pf = new PropertyFilter("GTD_tmPost",sdateStr);
		filters.add(pf);
		
		pf = new PropertyFilter("LED_tmPost",edateStr);
		filters.add(pf);
		
		newsPage = new Page<News>(1000);		
		newsPage = searchService.searchNews(newsPage, filters);	
		mblogPage = new Page<Mblog>(1000);		
		mblogPage = searchService.searchMblog(mblogPage,filters);
		bbsBlogPage = new Page<BbsBlog>(1000);		
		bbsBlogPage = searchService.searchBbsBlog(bbsBlogPage, filters);

		List<BaseNews> basenews = new ArrayList<BaseNews>();
		basenews.addAll(newsPage.getResult());
		basenews.addAll(mblogPage.getResult());
		basenews.addAll(bbsBlogPage.getResult());		
		Collections.sort(basenews);
		
		
		String contextPath = Struts2Utils.getSession().getServletContext().getRealPath("/");
		
		FileWriter fw = null;
		try {
			String dirStr = File.separator+"down"+File.separator+LoginUserUtil.getUser().getId();
			File file = new File(contextPath+File.separator+dirStr);
			if(!file.isDirectory()){
				file.mkdir();
			}
			
			String fileName = dirStr+File.separator+ sdateStr+"_"+edateStr+new Date().getTime()+"_all.csv";
			String filePath = contextPath+File.separator + fileName;
			fw = new FileWriter(filePath); 	
			
			
			
			String result = "标题,链接,发文媒体,发文时间,内容摘要\r\n";
			fw.write(result);
			fw.flush();
			for (BaseNews news : basenews) {		
				fw.write(news.formatBaseNewsForExport());
				fw.flush();
			}
			
			Report report = new Report();
			report.setContent(Struts2Utils.getParameter("content"));
			SimpleDateFormat formatDate = new SimpleDateFormat("yyyy-MM-dd");

			report.setSdate(formatDate.parse(sdateStr));
			report.setEdate(formatDate.parse(edateStr));
			report.setType(Struts2Utils.getParameter("type"));
			report.setUser(LoginUserUtil.getUser());
			report.setWords(Struts2Utils.getParameter("words"));
			report.setFile(fileName);
			report.setCdate(new Date());

			searchService.saveReport(report);

		} catch (Exception e) {
			logger.info("导出新闻出错" + e.getMessage(), e);
			e.printStackTrace();
		} finally {
			try {
				fw.close();
			} catch (IOException e) {
				logger.info("导出新闻出错" + e.getMessage(), e);
				e.printStackTrace();
			}
		}
	
		return null;
	}
	
	
	public String exportBbsblog2File() {

		List<PropertyFilter> filters = initFilters();
		String sdateStr = Struts2Utils.getParameter("sdate");
		String edateStr = Struts2Utils.getParameter("edate");
		PropertyFilter pf = new PropertyFilter("GTD_tmPost",sdateStr);
		filters.add(pf);
		
		pf = new PropertyFilter("LED_tmPost",edateStr);
		filters.add(pf);
		
		String contextPath = Struts2Utils.getSession().getServletContext().getRealPath("/");
		
		FileWriter fw = null;
		try {
			String dirStr = File.separator+"down"+File.separator+LoginUserUtil.getUser().getId();
			File file = new File(contextPath+File.separator+dirStr);
			if(!file.isDirectory()){
				file.mkdir();
			}
			
			String fileName = dirStr+File.separator+ sdateStr+"_"+edateStr+new Date().getTime()+"_bbsblog.csv";
			String filePath = contextPath+File.separator + fileName;
			fw = new FileWriter(filePath); 	
			
			bbsBlogPage = new Page<BbsBlog>(5000);
			// 设置默认排序方式
			if (!bbsBlogPage.isOrderBySetted()) {
				bbsBlogPage.setOrderBy("tmSpider");
				bbsBlogPage.setOrder(Page.DESC);
			}
			bbsBlogPage = searchService.searchBbsBlog(bbsBlogPage, filters);
			
			String result = "标题,链接,发文媒体,发文时间,浏览数,回复数,作者,作者信息,内容摘要\r\n";			
				
			fw.write(result);
			fw.flush();
			for (BbsBlog news : bbsBlogPage.getResult()) {				
				fw.write(news.formatForExport());
				fw.flush();
			}
			
			Report report = new Report();
			report.setContent(Struts2Utils.getParameter("content"));
			SimpleDateFormat formatDate = new SimpleDateFormat("yyyy-MM-dd");

			report.setSdate(formatDate.parse(sdateStr));
			report.setEdate(formatDate.parse(edateStr));
			report.setType(Struts2Utils.getParameter("type"));
			report.setUser(LoginUserUtil.getUser());
			report.setWords(Struts2Utils.getParameter("words"));
			report.setFile(fileName);
			report.setCdate(new Date());

			searchService.saveReport(report);

		} catch (Exception e) {
			logger.info("导出新闻出错" + e.getMessage(), e);
			e.printStackTrace();
		} finally {
			try {
				fw.close();
			} catch (IOException e) {
				logger.info("导出新闻出错" + e.getMessage(), e);
				e.printStackTrace();
			}
		}
	
		return null;
	}
	
	public String exportMblog2File() {

		List<PropertyFilter> filters = initFilters();
		String sdateStr = Struts2Utils.getParameter("sdate");
		String edateStr = Struts2Utils.getParameter("edate");
		PropertyFilter pf = new PropertyFilter("GTD_tmPost",sdateStr);
		filters.add(pf);
		
		pf = new PropertyFilter("LED_tmPost",edateStr);
		filters.add(pf);
		
		String contextPath = Struts2Utils.getSession().getServletContext().getRealPath("/");
		
		FileWriter fw = null;
		try {
			String dirStr = File.separator+"down"+File.separator+LoginUserUtil.getUser().getId();
			File file = new File(contextPath+File.separator+dirStr);
			if(!file.isDirectory()){
				file.mkdir();
			}
			
			String fileName = dirStr+File.separator+ sdateStr+"_"+edateStr+new Date().getTime()+"_mblog.csv";
			String filePath = contextPath+File.separator + fileName;
			fw = new FileWriter(filePath);  
			mblogPage = new Page<Mblog>(5000);
			// 设置默认排序方式
			if (!mblogPage.isOrderBySetted()) {
				mblogPage.setOrderBy("tmSpider");
				mblogPage.setOrder(Page.DESC);
			}
			mblogPage = searchService.searchMblog(mblogPage, filters);	
			//List<News> newsList = searchService.searchNews(filters);
			//String result = "标题,链接,发文媒体,发文时间,新闻内容摘要,相同新闻数,\r\n";
			String result = "发文人,链接,微博内容,发文时间,来源,转发数,评论数,收藏数,\r\n";
			fw.write(result);
			fw.flush();
			for (Mblog news : mblogPage.getResult()) {			
				fw.write(news.formatForExport());
				fw.flush();
			}
			
			Report report = new Report();
			report.setContent(Struts2Utils.getParameter("content"));
			SimpleDateFormat formatDate = new SimpleDateFormat("yyyy-MM-dd");

			report.setSdate(formatDate.parse(sdateStr));
			report.setEdate(formatDate.parse(edateStr));
			report.setType(Struts2Utils.getParameter("type"));
			report.setUser(LoginUserUtil.getUser());
			report.setWords(Struts2Utils.getParameter("words"));
			report.setFile(fileName);
			report.setCdate(new Date());

			searchService.saveReport(report);

		} catch (Exception e) {
			logger.info("导出微博出错" + e.getMessage(), e);
			e.printStackTrace();
		} finally {
			try {
				fw.close();
			} catch (IOException e) {
				logger.info("导出微博出错" + e.getMessage(), e);
				e.printStackTrace();
			}
		}
	
		return null;
	}
	
	
	public String exportNews2File() {

		List<PropertyFilter> filters = initFilters();
		String sdateStr = Struts2Utils.getParameter("sdate");
		String edateStr = Struts2Utils.getParameter("edate");
		PropertyFilter pf = new PropertyFilter("GTD_tmPost",sdateStr);
		filters.add(pf);
		
		pf = new PropertyFilter("LED_tmPost",edateStr);
		filters.add(pf);
		
		String contextPath = Struts2Utils.getSession().getServletContext().getRealPath("/");
		
		FileWriter fw = null;
		try {
			String dirStr = File.separator+"down"+File.separator+LoginUserUtil.getUser().getId();
			File file = new File(contextPath+File.separator+dirStr);
			if(!file.isDirectory()){
				file.mkdir();
			}
			
			String fileName = dirStr+File.separator+ sdateStr+"_"+edateStr+new Date().getTime()+"_news.csv";
			String filePath = contextPath+File.separator + fileName;
			fw = new FileWriter(filePath);  
			newsPage = new Page<News>(5000);
			// 设置默认排序方式
			if (!newsPage.isOrderBySetted()) {
				newsPage.setOrderBy("tmSpider");
				newsPage.setOrder(Page.DESC);
			}
			newsPage = searchService.searchNews(newsPage, filters);	
			//List<News> newsList = searchService.searchNews(filters);
			String result = "标题,链接,发文媒体,发文时间,新闻内容摘要,相同新闻数,\r\n";
			fw.write(result);
			fw.flush();
			for (News news : newsPage.getResult()) {				
				fw.write(news.formatForExport());
				fw.flush();
			}
			
			Report report = new Report();
			report.setContent(Struts2Utils.getParameter("content"));
			SimpleDateFormat formatDate = new SimpleDateFormat("yyyy-MM-dd");

			report.setSdate(formatDate.parse(sdateStr));
			report.setEdate(formatDate.parse(edateStr));
			report.setType(Struts2Utils.getParameter("type"));
			report.setUser(LoginUserUtil.getUser());
			report.setWords(Struts2Utils.getParameter("words"));
			report.setFile(fileName);
			report.setCdate(new Date());

			searchService.saveReport(report);

		} catch (Exception e) {
			logger.info("导出新闻出错" + e.getMessage(), e);
			e.printStackTrace();
		} finally {
			try {
				fw.close();
			} catch (IOException e) {
				logger.info("导出新闻出错" + e.getMessage(), e);
				e.printStackTrace();
			}
		}
	
		return null;
	}

}